package com.atguigu.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.client.DictFeignClient;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.repository.HospitalRepository;
import com.atguigu.yygh.service.HospitalService;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zr
 * @date 2021/12/12 16:10
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalRepository hospitalRepository;
    @Resource
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院接口
     *
     * @param paramMap
     */
    @Override
    public void saveHospital(Map<String, Object> paramMap) {
        // 将paramMap转换为对象
        // 首先将map转换为字符串
        String jsonString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        String hoscode = hospital.getHoscode();
        // 通过医院编号判断对象中的数据在数据库是否有值
        Hospital hospitalExists = hospitalRepository.getHospitalByHoscode(hoscode);
        // 有值就进行更新
        if (hospitalExists != null) {
            hospital.setStatus(hospitalExists.getStatus());
            hospital.setCreateTime(hospitalExists.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
        // 没值就进行添加
        hospital.setStatus(0);
        hospital.setCreateTime(new Date());
        hospital.setUpdateTime(new Date());
        hospital.setIsDeleted(0);
        hospitalRepository.save(hospital);

    }

    @Override
    public Hospital getHospitalByHosCode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 从MongoDB中查询数据
     *
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 构造条件查询
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Hospital hospital = new Hospital();

        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        ExampleMatcher exampleMatcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, exampleMatcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        // 获取查询中的list集合 遍历之后进行医院等级封装
        List<Hospital> content = pages.getContent();
        content.stream().forEach(item -> {
            this.setHospitalHospType(item);
        });
        return pages;
    }

    /**
     * 修改医院上线状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        // 首先根据医院id 查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        // 根据Id获取医院信息 包括医院等级
        Hospital hospital = this.setHospitalHospType(hospitalRepository.findById(id).get());
        Map<String, Object> result = new HashMap<>();
        result.put("hospital", hospital);
        result.put("bookingRule", hospital.getBookingRule());
        // 将hospital中的预约规则 设置为null 避免重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 根据医院名称查询
     *
     * @param hosName
     * @return
     */
    @Override
    public List<Hospital> findByHosName(String hosName) {
        return hospitalRepository.findHospitalByHosnameLike(hosName);
    }

    /**
     * 预约挂号详情
     *
     * @param hosCode
     * @return
     */
    @Override
    public Map<String, Object> getDepartDetail(String hosCode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHospType(this.getHospitalByHosCode(hosCode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }

    private Hospital setHospitalHospType(Hospital hospital) {
        // 根据dictCode和value 查询医院等级
        String hosTypeString = dictFeignClient.getDictName("Hostype", hospital.getHostype());
        // 获取省市区
        String provinceString = dictFeignClient.getDictName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getDictName(hospital.getCityCode());
        String districtString = dictFeignClient.getDictName(hospital.getDistrictCode());
        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        hospital.getParam().put("hosTypeString", hosTypeString);
        return hospital;
    }
}
